package com.woniuxy.store.service.impl;
import java.io.IOException;
import java.math.BigDecimal;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.store.common.StoreResultCode;
import com.woniuxy.store.infra.mapper.StoreApplayMapper;
import com.woniuxy.store.infra.model.StoreApplay;
import com.woniuxy.store.infra.model.StoreInfo;
import com.woniuxy.store.infra.model.StorePersonnel;
import com.woniuxy.store.infra.model.StoreWallet;
import com.woniuxy.store.service.*;
import com.woniuxy.store.service.param.SearchApplyServiceParam;
import com.woniuxy.store.service.util.QrCodeUtils;
import common.WoniuPage;
import enums.RoleEnum;
import exception.MallException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import result.CommonResultCodeEnum;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zz
 * @since 2023-07-20
 */
@Service
public class StoreApplayServiceImpl extends ServiceImpl<StoreApplayMapper, StoreApplay> implements StoreApplayService {
    @Resource
    private StoreInfoService storeInfoService;

    @Resource
    private StoreRoleService storeRoleService;

    @Autowired
    private QrCodeUtils qrCodeUtils;

    @Resource
    private StorePersonnelService personnelService;

    @Resource
    private StoreWalletService storeWalletService;

    @Override
    public void apply(StoreApplay storeApplay) {
        List<StoreApplay> list = list(new QueryWrapper<StoreApplay>().eq("applay_wechat_id", storeApplay.getApplayWechatId()).or().eq("applay_phone", storeApplay.getApplayPhone()));
        //判断改商家是否已经提交过申请
        if (list!=null&&list.size()!=0){
            throw new MallException("该手机号或微信号已被申请",StoreResultCode.APPLY_REPEAT);
        }
        storeApplay.setStoreProcessState("AUDIT");
        storeApplay.setApplayTime(DateTime.now());

        save(storeApplay);
    }

    @Override
    public StoreApplay handApply(Integer applayId,String storeProcessState) throws IOException {
        StoreApplay applay = getById(applayId);
        if (applay==null){
            throw new MallException(StoreResultCode.APPLY_NOT_EXSISTE);
        }
        //判断申请的状态
        if (applay.getStoreProcessState().equals(storeProcessState)){
            throw new MallException("已经是该状态了",CommonResultCodeEnum.INVALID_PARAM);
        }

        if (storeProcessState.equals("AUDIT")){
            throw new MallException(CommonResultCodeEnum.INVALID_PARAM);
        }

        if (storeProcessState.equals("PASS")){
            //申请通过后自动更新商家信息表
            StoreInfo storeInfo = BeanUtil.toBean(applay, StoreInfo.class);
            storeInfo.setStoreCreatTime(DateTime.now());
            storeInfo.setStoreTradeState("OPEN");
            storeInfo.setStoreState(RoleEnum.PROBATION.getMessage());
            storeInfo.setRoleId(RoleEnum.PROBATION.getCode());
            Date date =  new Date(DateTime.now().getTime()+(long)7*24*60*60*1000);
            storeInfo.setExpirationTime(date);
            storeInfoService.save(storeInfo);
            String code = qrCodeUtils.generateGeneralQrCode(storeInfo.getStoreId());
            storeInfo.setStoreQrCode(code);
            storeInfoService.updateById(storeInfo);
            //为申请人创建登录账号，身份默认为老板
            StorePersonnel personnel = new StorePersonnel();
            personnel.setStoreId(storeInfo.getStoreId());
            personnel.setStoreRoleId(1);
            personnel.setStoreRoleName("老板");
            personnel.setPersonnelName(applay.getApplayWechatId());
            personnel.setPersonnelPhone(applay.getApplayPhone());
            personnel.setPersonnelPassword(applay.getApplayPhone());
            personnelService.save(personnel);
        }

        applay.setStoreProcessState(storeProcessState);
        updateById(applay);

        return applay;
    }

    @Override
    public void modifyApply(StoreApplay storeApplay) {
        StoreApplay applay = getById(storeApplay.getApplayId());
        if (applay==null){
            throw new MallException(StoreResultCode.APPLY_NOT_EXSISTE);
        }
        if (applay.getStoreProcessState().equals("PASS")){
            throw new MallException(StoreResultCode.APPLY_PASSES);
        }
        storeApplay.setApplayTime(DateTime.now());
        updateById(storeApplay);

    }

    @Override
    public WoniuPage<StoreApplay> searchApply(SearchApplyServiceParam param) {
        QueryWrapper<StoreApplay> queryWrapper = new QueryWrapper<>();
        if (param.getApplayPhone()!=null&&param.getApplayWechatId()!=null&&!param.getApplayPhone().equals("")&&!param.getApplayWechatId().equals("")){
            queryWrapper.eq("applay_wechat_id",param.getApplayWechatId());
            queryWrapper.eq("applay_phone",param.getApplayPhone());
        }
        if (param.getStoreProcessState()!=null&&!param.getStoreProcessState().equals("")){
            queryWrapper.eq("store_process_state",param.getStoreProcessState());
        }
        if (param.getStoreType()!=null&&!param.getStoreType().equals("")){
            queryWrapper.eq("store_type",param.getStoreType());
        }
        Long pageIndex = 1L;
        Long pageSize = 3L;
        if (param.getPageSize()!=null&&param.getPageIndex()!=null){
            pageIndex = param.getPageIndex();
            pageSize = param.getPageSize();
        }
        Page<StoreApplay> page = new Page<StoreApplay>(pageIndex,pageSize);
        queryWrapper.orderByAsc("applay_time");
        Page<StoreApplay> page1 = page(page, queryWrapper);
        WoniuPage<StoreApplay> woniuPage = new WoniuPage<>();
        woniuPage.setPages(page1.getPages());
        woniuPage.setPageIndex(page1.getCurrent());
        woniuPage.setPageSize(page1.getSize());
        woniuPage.setTotal(page1.getTotal());
        woniuPage.setList(page1.getRecords());
        return woniuPage;
    }


}
